<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-autocomplete clearable v-model="queryForm.provinceName" class="filter-item" placeholder="省份" style="width: 125px;" :fetch-suggestions="querySearch" @select="handleSelectCity" @clear="clearProvince">
      </el-autocomplete>
      <el-autocomplete clearable v-model="queryForm.cityName" class="filter-item" placeholder="城市" style="width: 125px;" :fetch-suggestions="querySearchCity" @select="handleSelectDistrict" @clear="clearCity">
      </el-autocomplete>
      <el-autocomplete clearable v-model="queryForm.districtName" class="filter-item" placeholder="区" style="width: 125px;" :fetch-suggestions="querySearchDistrict" @select="handleSelectCommunity" @clear="clearCommunity">
      </el-autocomplete>
      <el-autocomplete clearable v-model="queryForm.communityName" class="filter-item" placeholder="小区" style="width: 125px;" :fetch-suggestions="querySearchCommunity" @select="handleSelectBuildings" @clear="clearBuilding">
      </el-autocomplete>
      <el-autocomplete clearable v-model="queryForm.buildName" class="filter-item" placeholder="楼栋" style="width: 125px;" :fetch-suggestions="querySearchBuildings" @select="handleSelectUnit" @clear="clearUnit">
      </el-autocomplete>
      <el-autocomplete clearable v-model="queryForm.unitName" class="filter-item" placeholder="单元" style="width: 125px;" :fetch-suggestions="querySearchUnit" @select="handleSelect" @clear="clearLast">
      </el-autocomplete> -->
      <el-select
        clearable
        v-model="queryForm.provinceId"
        class="filter-item"
        filterable
        placeholder="省份"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseProvince', 'query')"
      >
        <el-option
          v-for="(item, index) in provinces"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.cityId"
        class="filter-item"
        filterable
        placeholder="城市"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseCity', 'query')"
      >
        <el-option
          v-for="(item, index) in citys"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.districtId"
        class="filter-item"
        filterable
        placeholder="区"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseRegin', 'query')"
      >
        <el-option
          v-for="(item, index) in districts"
          :key="index"
          :value="item.rowId"
          :label="item.cnName"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.communityId"
        class="filter-item"
        filterable
        placeholder="小区"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseCommunity', 'query')"
      >
        <el-option
          v-for="(item, index) in communitys"
          :key="index"
          :value="item.rowId"
          :label="item.name"
        ></el-option>
      </el-select>
      <el-select
        clearable
        v-model="queryForm.buildId"
        class="filter-item"
        filterable
        placeholder="楼栋"
        style="width: 125px;"
        @change="value => cascade(value, 'chooseBuliding', 'query')"
      >
        <el-option
          v-for="(item, index) in buildings"
          :key="index"
          :value="item.rowId"
          :label="item.name"
        ></el-option>
      </el-select>
      <el-select
        v-model="queryForm.unitId"
        class="filter-item"
        placeholder="单元"
        filterable
        style="width: 125px;"
        @change="value => cascade(value, 'chooseUnit', 'query')"
      >
        <el-option
          v-for="(item, index) in units"
          :key="index"
          :value="item.rowId"
          :label="item.name"
        ></el-option>
      </el-select>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入房屋门牌号"
        v-model.trim="queryForm.number"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入姓名"
        v-model="queryForm.realName"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入手机号"
        v-model="queryForm.phone"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入身份证"
        v-model="queryForm.idcard"
      >
      </el-input>
      <el-select
        clearable
        v-model="queryForm.relationType"
        class="filter-item"
        placeholder="住户类型"
        style="width: 125px;"
      >
        <el-option
          v-for="(val, key) in Dicts.HOUSE_USER_RELATION_TYPE"
          :key="key"
          :value="key"
          :label="val"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doSearchQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column align="center" label="省/市/区" width="450">
        <template slot-scope="scope">
          {{
            `${scope.row.provinceName}/${scope.row.cityName}/${scope.row.districtName}`
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="communityName"
        label="小区"
        align="center"
        width="250"
      >
      </el-table-column>
      <el-table-column label="大楼/单元/室" align="center" width="250">
        <template slot-scope="scope">
          <template v-if="scope.row.floorName !== undefined">
            {{
              `${scope.row.buildName}栋/${scope.row.unitName}单元/${scope.row.floorName}层/${scope.row.number}室`
            }}
          </template>
          <template v-if="scope.row.floorName === undefined">
            {{
              `${scope.row.buildName}栋/${scope.row.unitName}单元/${scope.row.number}室`
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="姓名" align="center" width="150">
      </el-table-column>
      <el-table-column prop="phone" label="电话" align="center" width="250">
        <template slot-scope="scope">
          <template v-if="scope.row.phone !== undefined">
            {{
              scope.row.phone.substr(0, 3) + '*****' + scope.row.phone.slice(8)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="idcard" label="身份证" align="center" width="250">
        <template slot-scope="scope">
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 15
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(11)
            }}
          </template>
          <template
            v-if="
              scope.row.idcard !== undefined && scope.row.idcard.length == 18
            "
          >
            {{
              scope.row.idcard.substr(0, 4) +
                '******' +
                scope.row.idcard.slice(14)
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="relationType"
        label="住户类型"
        align="center"
        width="150"
      >
        <template slot-scope="scope">
          {{ Dicts.HOUSE_USER_RELATION_TYPE[scope.row.relationType] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="isAdmin"
        label="是否管理员"
        align="center"
        width="150"
      >
        <template slot-scope="scope">
          {{ Dicts.RESIDENT_IS_ADMIN[scope.row.isAdmin] }}
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryResidentByPage,
  selectAllCommunity,
  selectByCommunityIdBuilding,
  selectByBuildingUnit
} from '@/api/community'
import { queryProvince, getCity, getDistrict } from '@/api/area'
export default {
  name: 'resident',
  data() {
    return {
      loading: false,
      provinces: [],
      citys: [],
      districts: [],
      communitys: [],
      buildings: [],
      units: [],
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      statusOptions: Dicts.RESIDENT_IS_ADMIN,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      this.queryForm.isDeleted = 0
      queryResidentByPage(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
      queryProvince().then(({ data: { data } }) => {
        this.provinces = data
        this.loading = false
      })
    },
    doSearchQuery() {
      this.queryForm.current = 1
      this.doQuery()
    },
    cascade(rowId, level, param) {
      if (param === 'query') {
        if (rowId !== '') {
          switch (level) {
            case 'chooseProvince':
              getCity(rowId).then(({ data: { data } }) => {
                this.queryForm.cityId = ''
                this.queryForm.districtId = ''
                this.queryForm.communityId = ''
                this.queryForm.buildId = ''
                this.queryForm.unitId = ''
                this.citys = []
                this.districts = []
                this.communitys = []
                this.buildings = []
                this.units = []
                this.citys = data
              })
              return
            case 'chooseCity':
              getDistrict(rowId).then(({ data: { data } }) => {
                this.queryForm.districtId = ''
                this.queryForm.communityId = ''
                this.queryForm.buildId = ''
                this.queryForm.unitId = ''
                this.districts = []
                this.communitys = []
                this.buildings = []
                this.units = []
                this.districts = data
              })
              return
            case 'chooseRegin':
              selectAllCommunity(rowId).then(({ data: { data } }) => {
                this.queryForm.communityId = ''
                this.queryForm.buildId = ''
                this.queryForm.unitId = ''
                this.communitys = []
                this.buildings = []
                this.units = []
                this.communitys = data
              })
              return
            case 'chooseCommunity':
              selectByCommunityIdBuilding(rowId).then(({ data: { data } }) => {
                this.queryForm.buildId = ''
                this.queryForm.unitId = ''
                this.buildings = []
                this.units = []
                this.buildings = data
              })
              return
            case 'chooseBuliding':
              selectByBuildingUnit(rowId).then(({ data: { data } }) => {
                this.queryForm.unitId = ''
                this.units = []
                this.units = data
              })
              return
            case 'chooseUnit':
              selectByBuildingUnit(this.queryForm.buildId).then(
                ({ data: { data } }) => {
                  this.units = data
                }
              )
              return
          }
        } else {
          switch (level) {
            case 'chooseProvince':
              this.queryForm.cityId = ''
              this.queryForm.districtId = ''
              this.queryForm.communityId = ''
              this.queryForm.buildId = ''
              this.queryForm.unitId = ''
              this.citys = []
              this.districts = []
              this.communitys = []
              this.buildings = []
              this.units = []
              return
            case 'chooseCity':
              this.queryForm.districtId = ''
              this.queryForm.communityId = ''
              this.queryForm.buildId = ''
              this.queryForm.unitId = ''
              this.districts = []
              this.communitys = []
              this.buildings = []
              this.units = []
              return
            case 'chooseRegin':
              this.queryForm.communityId = ''
              this.queryForm.buildId = ''
              this.queryForm.unitId = ''
              this.communitys = []
              this.buildings = []
              this.units = []
              return
            case 'chooseCommunity':
              this.queryForm.buildId = ''
              this.queryForm.unitId = ''
              this.buildings = []
              this.units = []
              return
            case 'chooseBuliding':
              this.queryForm.unitId = ''
              this.units = []
              return
          }
        }
      }
    }
  },
  components: {}
}
</script>

<style scoped></style>
